package com.gzlz.budejie.activity;

import java.util.List;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gzlz.budejie.R;
import com.gzlz.budejie.R.id;
import com.gzlz.budejie.R.layout;
import com.gzlz.budejie.bean.SubscribeBean;
import com.gzlz.budejie.bean.SubscribeBean.RecommendData;
import com.gzlz.budejie.bean.SubscribeBean.SubscribedData;
import com.gzlz.budejie.utils.BitmapCacheUtils;
import com.gzlz.budejie.utils.NetCacheUtils;
import com.gzlz.budejie.utils.SP;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class SubscribeActivity extends Activity implements OnClickListener {

	private ImageButton iv_back_sub;
	private TextView tv_title_sub;
	private ImageButton ib_add_sub;
	private ImageView ib_search_sub;
	private ListView lv_sub;
	private RelativeLayout rl_sub;
	
	private String url;
	
	private List<SubscribedData> def_tags;
	public List<RecommendData> rec_tags;
	private BitmapUtils bitmapUtils;
	private SP sp;
	private final String JSON = "json";
	private String json;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_subscribe);
		initView();
		initdata();
		bitmapUtils = new BitmapUtils(SubscribeActivity.this);
		bitmapUtils.configDefaultBitmapConfig(Config.ARGB_4444);
	}

	private void initView() {
		rl_sub = (RelativeLayout) findViewById(R.id.rl_sub);
		iv_back_sub = (ImageButton) findViewById(R.id.iv_back_sub);
		tv_title_sub = (TextView) findViewById(R.id.tv_title_sub);
		ib_add_sub = (ImageButton) findViewById(R.id.ib_add_sub);
		ib_search_sub = (ImageView) findViewById(R.id.ib_search_sub);
		lv_sub = (ListView) findViewById(R.id.lv_sub);
		
		iv_back_sub.setOnClickListener(this);
		ib_add_sub.setOnClickListener(this);
		ib_search_sub.setOnClickListener(this);
	}

	private void initdata() {
		url = "https://api.budejie.com/api/api_open.php?c=subscribe&a=tags_list";
		json = SP.getString(url);
		if (!TextUtils.isEmpty(json)) {
			processData(json);
			// Log.e("TAG", "Json" + json);
		}
		getDataFromNet();
	}

	/**
	 * 请求网络
	 */
	private void getDataFromNet() {

		HttpUtils httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				// 缓存数据
				SP.putString(url, responseInfo.result);
				// 解析和处理数据
				processData(responseInfo.result);
				Log.e("TAG", "网络连接成功了：" + responseInfo.result);

			}

			@Override
			public void onFailure(HttpException error, String msg) {
				Log.e("TAG", "网络连接失败：" + msg);

			}
		});

	}

	/**
	 * 解析和处理数据
	 * 
	 * @param json
	 */
	protected void processData(String json) {
		// Log.e("TAG", "processData");
		Gson gson = new Gson();
		SubscribeBean subscribedBean = gson.fromJson(json, SubscribeBean.class);
		Log.e("TAG", "解析成功了---" + subscribedBean.def_tags.get(0).theme_name);
		def_tags = subscribedBean.def_tags;
		rec_tags = subscribedBean.rec_tags;
		lv_sub.setAdapter(new SubAdapter());

	}

	class SubAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return def_tags.size() + rec_tags.size() + 2;
		}
		@Override
		public Object getItem(int position) {
			if (position == 0) {
				return rec_tags.size();
			} else if (position >= 1 && position <= rec_tags.size()) {
				return rec_tags.get(position - 1);
			} else if (position == rec_tags.size() + 1) {
				return def_tags.size();
			} else {
				return def_tags.get(position - rec_tags.size() - 2);
			}
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (position == 0) {
				RelativeLayout rl = (RelativeLayout) View.inflate(
						SubscribeActivity.this, R.layout.sbu_textview, null);
				TextView textName = (TextView) rl
						.findViewById(R.id.sub_tv_name);
				TextView textMore = (TextView) rl
						.findViewById(R.id.sub_tv_more);
				textName.setText("推荐标签");
				rl.setOnClickListener(SubscribeActivity.this);
				return rl;
			} else if (position == rec_tags.size() + 1) {
				RelativeLayout rl = (RelativeLayout) View.inflate(
						SubscribeActivity.this, R.layout.sbu_textview, null);
				TextView textName = (TextView) rl
						.findViewById(R.id.sub_tv_name);
				TextView textMore = (TextView) rl
						.findViewById(R.id.sub_tv_more);
				textName.setText("已订阅标签");
				textMore.setText("       ");
				return rl;
			}

			ViewHolder holder=null;
			if (convertView==null || convertView instanceof RelativeLayout) {
				holder = new ViewHolder();
				convertView = View.inflate(SubscribeActivity.this,
						R.layout.item_sbuscribe, null);
				holder.iv_icon = (ImageView) convertView
						.findViewById(R.id.iv_sbu_icon);
				holder.tv_name = (TextView) convertView.findViewById(R.id.tv_sbu_name);
				holder.tv_number = (TextView) convertView
						.findViewById(R.id.tv_sbu_nub);
				holder.btn_sbu = (Button) convertView.findViewById(R.id.btn_sbu);
				Log.e("TAG", "holder.tv_name" + holder.tv_name);
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			if (position > 0 && position < rec_tags.size() + 1) {
				RecommendData data = rec_tags.get(position-1);
				holder.tv_name.setText(data.theme_name);
				Log.e("TAG", "holder.tv_name");
				holder.tv_number.setText(data.sub_number + "人订阅");
				holder.btn_sbu.setText("+订阅");
				holder.btn_sbu.setOnClickListener(SubscribeActivity.this);
				bitmapUtils.display(holder.iv_icon, data.image_list);
				return convertView;
			} else {
				SubscribedData data = def_tags.get(position-rec_tags.size()-2);
				holder.tv_name.setText(data.theme_name);
				Log.e("TAG", "holder.tv_name");
				holder.tv_number.setText(data.sub_number + "人订阅");
				holder.btn_sbu.setText("取消订阅");
				bitmapUtils.display(holder.iv_icon, data.image_list);
				return convertView;
			}
		}


	}

	static class ViewHolder {
		ImageView iv_icon;
		TextView tv_name;
		TextView tv_number;
		Button btn_sbu;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_back_sub:
			finish();
			break;
		case R.id.ib_add_sub:
			Intent intent = new Intent(getApplicationContext(), SubscribeAddActivity.class);
			intent.putExtra(JSON, json);
			startActivity(intent);
			break;
		case R.id.ib_search_sub:
			startActivity(new Intent(getApplicationContext(), SubscribeSearchActivity.class));
			break;
		case R.id.btn_sbu:
			startActivity(new Intent(getApplicationContext(), LoginActivity.class));
			break;
		case R.id.rl_sbu_text:
		Intent intent2 = new Intent(getApplicationContext(), SubscribeAddActivity.class);
		intent2.putExtra(JSON, json);
			startActivity(intent2);
			break;

		default:
			break;
		}
	}

}
